﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ShootThemUp
{
    class Menu : GameState
    {
        const int nbItem = 4;
        private string[] texts;
        private float[] selectionFade;
        private Vector2[] position;
        private int selected;
        private SpriteFont font;
        private static KeyboardState prevKbState;

        public Menu()
        {
            texts = new string[] { "Nouvelle partie", "Options", "Credits", "Quitter" };
            position = new Vector2[nbItem];
            int k = 100;
            for (int i = 0; i < nbItem; i++)
            {
                position[i] = new Vector2(100, k);
                k += 100;
            }
            selectionFade = new float[nbItem];
            selected = 0;
            prevKbState = Keyboard.GetState();
        }

        public override void Initialize()
        {
            //throw new NotImplementedException();
        }

        public override void Load(ContentManager content)
        {
            font = content.Load<SpriteFont>("ScoreFont");
        }

        public override void Update(GameTime gt)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Down) && prevKbState !=  Keyboard.GetState())
                if (selected < nbItem - 1) selected++;
                else selected = 0;

            if (Keyboard.GetState().IsKeyDown(Keys.Up) && prevKbState != Keyboard.GetState())
                if (selected > 0) selected--;
                else selected = nbItem - 1;

            float fadeSpeed = (float)gt.ElapsedGameTime.TotalSeconds * 4;
            for (int i = 0; i < texts.Length; i++)
            {
                if (selected == i)
                    selectionFade[i] = Math.Min(selectionFade[i] + fadeSpeed, 1);
                else
                    selectionFade[i] = Math.Max(selectionFade[i] - fadeSpeed, 0);
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Enter) && prevKbState != Keyboard.GetState())
            {
                StateChange("PlayingState");
            }
            prevKbState = Keyboard.GetState();
        }

        public override void Draw(SpriteBatch sb, GameTime gt)
        {
            for (int i = 0; i < texts.Length; i++)
            {
                Color color = Color.White;
                // Draw the selected entry in yellow, otherwise white.
                if (selected == i)
                    color = Color.Yellow;

                // Pulsate the size of the selected menu entry.
                double time = gt.TotalGameTime.TotalSeconds;
                float pulsate = (float)Math.Sin(time * 6) + 1;
                float scale = 1 + pulsate * 0.05f * selectionFade[i];

                //// Modify the alpha to fade text out during transitions.
                //color *= screen.TransitionAlpha;

                //// Draw text, centered on the middle of each line.
                //ScreenManager screenManager = screen.ScreenManager;
                Vector2 origin = new Vector2(0, font.LineSpacing / 2);
                sb.Begin();
                sb.DrawString(font, texts[i], position[i], color, 0,
                                       origin, scale, SpriteEffects.None, 0);
                sb.End();
            }
        }

        public override void Unload()
        {
            //throw new NotImplementedException();
        }

        public override void Finalize()
        {
            //throw new NotImplementedException();
        }
    }
}
